package com.kye.express.business.common_tool_new;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.base.BaseAppCompatActivity;
import com.kye.express.business.common_tool_new.viewbinder.CommonToolEditItemViewBinder;
import com.kye.express.business.common_tool_new.viewbinder.CommonToolNormalItemViewBinder;
import com.kye.express.business.common_tool_new.viewmodel.CommonToolEditModel;
import com.kye.express.business.common_tool_new.viewmodel.CommonToolNormalModel;
import com.kye.express.databinding.ActivityCommonToolNewBinding;

import java.util.List;

import io.reactivex.functions.Consumer;
import me.drakeet.multitype.MultiTypeAdapter;

public class CommonToolActivity extends BaseAppCompatActivity implements CommonToolContract.CommonToolView {

    private final static String TAG_STATUS_TYPE = "TAG_STATUS_TYPE";
    private int statusType = CommonToolStatusType.STATUS_CONTENT;
    private ActivityCommonToolNewBinding binding;
    private CommonToolContract.CommonToolPresenter presenter;
    private MultiTypeAdapter selectedNormalAdapter;
    private MultiTypeAdapter selectedEditAdapter;
    private MultiTypeAdapter unselectedEditAdapter;

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, CommonToolActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            savedInstanceState.getInt(TAG_STATUS_TYPE, CommonToolStatusType.STATUS_EMPTY);
        }
        binding = DataBindingUtil.setContentView(this, R.layout.activity_common_tool_new);
        presenter = new CommonToolContract.CommonToolPresenter();
        presenter.bind(this);
        presenter.onCreate(savedInstanceState);
        initViews(savedInstanceState);
        initEvents(savedInstanceState);
        initData(savedInstanceState);
    }

    private void initViews(Bundle savedInstanceState) {

        selectedNormalAdapter = new MultiTypeAdapter();
        selectedNormalAdapter.register(CommonToolNormalModel.class, new CommonToolNormalItemViewBinder(presenter));
        selectedNormalAdapter.setHasStableIds(true);
        binding.selectedCommonToolsNormal.setLayoutManager(new GridLayoutManager(this, 4));
        binding.selectedCommonToolsNormal.setAdapter(selectedNormalAdapter);

        selectedEditAdapter = new MultiTypeAdapter();
        selectedEditAdapter.setHasStableIds(true);
        selectedEditAdapter.register(CommonToolEditModel.class, new CommonToolEditItemViewBinder(presenter));
        binding.selectedCommonToolsEdit.setLayoutManager(new GridLayoutManager(this, 4));
        binding.selectedCommonToolsEdit.setAdapter(selectedEditAdapter);

        unselectedEditAdapter = new MultiTypeAdapter();
        unselectedEditAdapter.setHasStableIds(true);
        unselectedEditAdapter.register(CommonToolEditModel.class, new CommonToolEditItemViewBinder(presenter));
        binding.unselectedCommonToolsEdit.setLayoutManager(new GridLayoutManager(this, 4));
        binding.unselectedCommonToolsEdit.setAdapter(unselectedEditAdapter);

    }

    private void initEvents(Bundle savedInstanceState) {
        RxView.clicks(binding.optionAddFinish)
                .compose(new DebounceObservableTransformer<Object>())
                .compose(this.bindToLifecycleDestroy())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        switch (statusType) {
                            case CommonToolStatusType.STATUS_CONTENT:
                                showPageEdit();
                                break;
                            case CommonToolStatusType.STATUS_EDIT:
                                presenter.updateCommonToolsData();
                                break;
                            default:
                                showPageEdit();
                                break;
                        }
                    }
                });
    }

    @Override
    public void updateNormalSelectedCommonTools(List<CommonToolNormalModel> commonToolNormalModels) {
        selectedNormalAdapter.setItems(commonToolNormalModels);
        selectedNormalAdapter.notifyDataSetChanged();
//        selectedNormalAdapter.notifyItemRangeChanged(0, commonToolNormalModels.size());
    }

    @Override
    public void updateEditSelectedCommonTools(List<CommonToolEditModel> commonToolEditModels) {
        selectedEditAdapter.setItems(commonToolEditModels);
        selectedEditAdapter.notifyDataSetChanged();
//        selectedNormalAdapter.notifyItemRangeChanged(0, commonToolEditModels.size());
    }

    @Override
    public void updateEditUnselectedCommonTools(List<CommonToolEditModel> commonToolEditModels) {
        unselectedEditAdapter.setItems(commonToolEditModels);
        unselectedEditAdapter.notifyDataSetChanged();
//        selectedNormalAdapter.notifyItemRangeChanged(0, commonToolEditModels.size());
    }

    private void initData(Bundle savedInstanceState) {
        presenter.initCommonToolsData();
    }

    @Override
    public void showPageEmpty() {
        statusType = CommonToolStatusType.STATUS_EMPTY;
        binding.setStatus(statusType);
    }

    @Override
    public void showPageContent() {
        statusType = CommonToolStatusType.STATUS_CONTENT;
        binding.setStatus(statusType);
    }

    @Override
    public void showPageEdit() {
        statusType = CommonToolStatusType.STATUS_EDIT;
        binding.setStatus(statusType);
    }

    @Override
    public MultiTypeAdapter getSelectedNormalAdapter() {
        return selectedNormalAdapter;
    }

    @Override
    public MultiTypeAdapter getSelectedEditAdapter() {
        return selectedEditAdapter;
    }

    @Override
    public MultiTypeAdapter getUnselectedEditAdapter() {
        return unselectedEditAdapter;
    }
}
